﻿

using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Common.Models;
using Store.Manager.ERP.Domain.Entities;
using Store.Manager.ERP.Domain.Repositories;

namespace Store.Manager.ERP.Application.Services
{
    public class OrganizationService : IOrganizationService
    {
        private readonly IOrganizationRepository _organizationRepository;
        public OrganizationService(IOrganizationRepository organizationRepository)
        {
            _organizationRepository = organizationRepository;
        }
        public async Task<CommonResponse<OrganizationDto>> Add(OrganizationDto organizationDto)
        {
            var organization = new Organization
            {
                Code = organizationDto.Code,
                Name = organizationDto.Name,
                ParentCode = organizationDto.ParentCode,
                Address = organizationDto.Address,
                ContactPerson = organizationDto.ContactPerson,
                ContactPhone = organizationDto.ContactPhone
            };
            var dbResult = await _organizationRepository.AddAsync(organization);
            if (dbResult.Success)
            {
                return CommonResponse<OrganizationDto>.OK(organizationDto, "添加成功");
            }
            else
                return CommonResponse<OrganizationDto>.Error("添加失败");
        }

        public async Task<bool> AddOrganizations(List<OrganizationDto> organizationDtos)
        {
            var organizations = organizationDtos.Select(dto => new Organization
            {
                Code = dto.Code,
                Name = dto.Name,
                ParentCode = dto.ParentCode,
                Address = dto.Address,
                ContactPerson = dto.ContactPerson,
                ContactPhone = dto.ContactPhone
            }).ToList();

            return await _organizationRepository.AddRangeAsync(organizations);
        }

        public async Task Delete(string code)
        {
            await _organizationRepository.DeleteAsync(code);
        }

        public async Task<List<OrganizationDto>> GetAll()
        {
            var organizations = await _organizationRepository.GetAllAsync();
            return organizations.Select(o => new OrganizationDto
            {
                Code = o.Code,
                Name = o.Name,
                ParentCode = o.ParentCode,
                Address = o.Address,
                ContactPerson = o.ContactPerson,
                ContactPhone = o.ContactPhone
            }).ToList();
        }

        public async Task<OrganizationDto> GetById(string code)
        {
            var organization = await _organizationRepository.GetByIdAsync(code);
            if (organization != null)
            {
                return new OrganizationDto
                {
                    Code = organization.Code,
                    Name = organization.Name,
                    ParentCode = organization.ParentCode,
                    Address = organization.Address,
                    ContactPerson = organization.ContactPerson,
                    ContactPhone = organization.ContactPhone
                };
            }
            return null;
        }

        public async Task Update(OrganizationDto organizationDto)
        {
            var organization = await _organizationRepository.GetByIdAsync(organizationDto.Code);
            if (organization != null)
            {
                organization.Name = organizationDto.Name;
                organization.ParentCode = organizationDto.ParentCode;
                organization.Address = organizationDto.Address;
                organization.ContactPerson = organizationDto.ContactPerson;
                organization.ContactPhone = organizationDto.ContactPhone;
                await _organizationRepository.UpdateAsync(organization);
            }
        }
    }
}
